package com.zx._12_算法;


public class ListNode {

    public int val;
    public ListNode next;


    public ListNode() {
    }

    public ListNode(int x) {
        val = x;
    }

    public ListNode(int val, ListNode next) {
        super();
        this.val = val;
        this.next = next;
    }

    public static ListNode getNodes(Integer... nums) {
        ListNode result = new ListNode();
        result.val = nums[0];

        ListNode node = result;
        for (int i = 1; i < nums.length; i++) {
            ListNode node2 = new ListNode();
            node2.val = nums[i];
            node.next = node2;
            node = node2;
        }
        return result;
    }

    public ListNode get(int index) {
        ListNode curr = this;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr;
    }

    public void print() {
        ListNode head = this;
        String str = "";
        while (head != null) {
            str += head.val + ",";
            head = head.next;
        }
        System.out.println(str.substring(0, str.length() - 1));
    }

}
